import { DateTime } from "luxon";

export class DateTimeHelper {

    /// <summary>
    /// datetime转换为unixtime(秒) 
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    static ToUnixTime(dateTime: DateTime | Date): number {
        if (!dateTime) {
            return 0;
        }

        if (dateTime instanceof Date) {
            return  DateTime.fromJSDate(dateTime).toLocal().toSeconds();
        }

        //return (dateTime.toLocal().diff(DateTime.utc(1970, 1, 1)).milliseconds) / 1000;
        return dateTime.toSeconds();
    }

    /// <summary>
    /// datetime转换为unixtime(毫秒) 
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    static ToUnixTimeByMilliseconds(dateTime: DateTime | Date): number {
        if (!dateTime) {
            return 0;
        }
        if (dateTime instanceof Date) {
            return DateTime.fromJSDate(dateTime).toLocal().toMillis();
        }
        return dateTime.toMillis();

        //let locolData = dateTime.toLocal(); //如果传入的时间没有时区， toLocal是否是不改变字面量而直接设置时区
        //let start = DateTime.utc(1970, 1, 1);
        //return locolData.diff(start).milliseconds
    }

    /// <summary>
    /// unix时间转换为datetime（已转本地时间）
    /// </summary>
    /// <param name="timeStamp">单位（毫秒）</param>
    /// <returns></returns>
    static FromUnixTimestampByMilliseconds(timeStamp: number): DateTime {
        let date = DateTime.fromMillis(timeStamp);

        if (abp.clock.provider.supportsMultipleTimezone) {
            return date.setZone(abp.timing.timeZoneInfo.iana.timeZoneId);
        } else {
            return date;
        }
    }

}
